<?php
//$Id: Base.php 605 2012-12-15 05:07:41Z tomqin@gmail.com $

/**
 * Part of the framework.
 *
 * @package     Library
 * @version     1.0
 * @copyright   Copyright (C) 2010-2012 SupeFun.com Inc.
 * @author      TomQin <tomqin@gmail.com>
 * @license     http://www.supefun.com/license/
 */

if(!defined('CONF_PATH')) {
    die('CONF_PATH is not defined');
}

final class Base
{

    /**
     * 对象缓存
     */
    protected static $_objects = array();

    /**
     * 取得一个对象
     */
    static public function getObject($class) {
        if (isset(self::$_objects[$class])) {
            return self::$_objects[$class];
        }else{
            $classname  = Base::import($class);
            $object = new ReflectionClass($classname);
            if(func_num_args() >= 2) {
                $args = func_get_args();
                array_shift($args);
                $object = $object->newInstanceArgs($args);
            }else{
                $object = $object->newInstance();
            }
            self::$_objects[$class] = $object;
        }
        return self::$_objects[$class];
    }

    /**
     * 载入一个类
     */
    static public function import($path) {
        $paths = explode('.', $path);
        $class = $paths[count($paths) - 1];
        if(!class_exists($class, false)) {
            if(in_array($paths[0], array('module', 'controller', 'service'))) {
                $folder = 'source';
            }else{
                $folder = 'library';
            }
            $file = BASE_PATH . $folder . DS . str_replace('.', DS, $path) . '.php';
            if(is_file($file)) {
                require_once $file;
            }else{
                throw new Exception(sprintf('No class found of `%s`.', $path));
            }
        }
        return $class;
    }

    /**
     * 配置缓存
     */
    static protected $_configs = array();
    static protected $_config_cache = array();

    /**
     * 载入一个配置
     */
    static public function loadConfig($config) {
        if(is_string($config)) {
            $file = CONF_PATH . $config . '.php';
            if(is_file($file))
                self::$_configs[$config] = include($file);
        }
    }

    /**
     * 保存一个配置
     */
    static public function saveConfig($file, array $config) {
        $file = CONF_PATH . $file . '.php';
        $string = "<?php\r\nreturn " .  var_export($config, true)  . ";\r\n?>";
        return file_put_contents($file, $string);
    }

    /**
     * 取得一个配置项目
     */
    static public function getConfig($item, $default = null) {
        if (isset(self::$_configs[$item])) {
            return self::$_configs[$item];
        }elseif (!isset(self::$_config_cache[$item])) {
            list($group, ) = explode('.', $item, 2);
            self::loadConfig($group);
            self::$_config_cache[$item] = Arr::get(self::$_configs, $item, $default);
        }

        return self::$_config_cache[$item];
    }

    /**
     * 设置一个配置项目
     */
    static public function setConfig($item, $value) {
        strpos($item, '.') === false or self::$_config_cache[$item] = $value;
        return Arr::set(self::$_configs, $item, $value);
    }

    /**
     * 删除一个配置项目
     */
    static public function delConfig($item) {
        if (isset(self::$_config_cache[$item])) {
            unset(self::$_config_cache[$item]);
        }
        return Arr::delete(self::$_configs, $item);
    }

    /**
     * 判断 POST/GET
     */
    static public function getRequestMethod() {
        if($_SERVER['REQUEST_METHOD'] == 'POST') return 'post';
        else return 'get';
    }

    /**
     * 判断是否为 ajax
     */
    static public function isAjaxRequest() {
        return ($_SERVER['HTTP_X_REQUESTED_WITH'] == 'XMLHttpRequest');
    }
}
